package com.tgy.leetcode;

import java.util.Arrays;

public class _1423_可获得的最大点数 {

    public static int maxScore(int[] cardPoints, int k) {

        int max = 0;
        if (cardPoints.length == k) {

            for (int cardPoint : cardPoints) {
                max += cardPoint;
            }
            return max;
        }


        int sum = 0;
        int len = cardPoints.length;
        int[] nums = new int[k<<1];
        for (int i = 0; i < k; i++) {

            nums[i] = cardPoints[len + i - k];
            nums[i+k] = cardPoints[i];
            sum += nums[i];
        }

        max = sum;
        for (int i = k; i < nums.length; i++) {

            sum += nums[i];
            sum -= nums[i - k];
            max = Math.max(max,sum);
        }
        return max;
    }

    public static void main(String[] args) {

//        int[] nums = {1, 2, 3, 4, 5, 6, 1};
        int[] nums = {1,79,80,1,1,1,200,1};
        int k = 4;
        int result = maxScore( nums, k);

        System.out.println(result);
    }
}
